<template>
  <div class="content">
    <el-card class="content-card">
      <div class="refresh"><el-button type="primary" size="small" icon="el-icon-refresh-left" @click="refresh">刷新</el-button></div>
      <el-table
        v-loading="loading"
        :data="logApiList"
        style="width: 100%"
        row-key="id"
        border
        lazy
      >
        <el-table-column align="center" prop="createTime" label="请求时间" width="160" />
        <el-table-column align="center" prop="title" label="名称" width="140" />
        <el-table-column align="center" prop="requestUri" label="请求URL" />
        <el-table-column align="center" prop="method" label="请求方式" width="80">
          <template slot-scope="scope">
            <el-tag :type="tagTypes[scope.row.method]">{{ scope.row.method }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="params" label="参数" width="120" />
        <el-table-column align="center" prop="time" label="请求时长(毫秒)" width="120" />
        <el-table-column align="center" prop="remoteIp" label="请求IP" width="100" />
        <el-table-column align="center" prop="methodName" label="请求方法名称" width="120" />
        <el-table-column align="center" prop="methodClass" label="请求类" width="180" />
        <el-table-column align="center" prop="userAgent" label="客户端类型" />
        <el-table-column align="center" prop="result" label="响应结果" />
      </el-table>
    </el-card>
    <el-pagination
      v-if="page.pages"
      background
      layout="prev, pager, next"
      :current-page.sync="params.current"
      :page-size="page.size"
      :page-count="page.pages"
      @current-change="turnPage"
    />
  </div>
</template>

<script>
import { getLogApiList } from '@/api/logApi'

const tagTypes = { 'GET': 'success', 'POST': '', 'PUT': 'warning', 'DELETE': 'danger' }

export default {
  data() {
    return {
      loading: false,
      params: {
        current: 1,
        size: 10,
        parentId: 0
      },
      page: {
        current: 1,
        size: 10
      },
      logApiList: [],
      tagTypes: tagTypes
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取meta列表
    fetchData() {
      this.loading = true
      getLogApiList(this.params).then(response => {
        if (response.code === 200) {
          this.logApiList = response.data.records
          this.page.current = response.data.current
          this.page.size = response.data.current
          this.page.total = response.data.total
          this.page.pages = response.data.pages
        } else {
          this.$message({
            type: 'error',
            message: response.message
          })
        }
        this.loading = false
      })
    },
    // 翻页
    turnPage() {
      this.fetchData()
    },
    // 刷新
    refresh() {
      this.fetchData()
    }
  }
}
</script>
